﻿Public Class frmXML

    Dim ds As New DataSet

    Private Sub frmXML_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim mode As System.Data.XmlReadMode = ds.ReadXml("C:\Basins\data\03070101-1\STORET\01008001.STORETresults")
        For i As Integer = 0 To ds.Relations.Count - 1
            With ds.Relations(i)
                Debug.Print(.RelationName & ": " & .ParentTable.TableName & "," & .ChildTable.TableName)
            End With
        Next
        Dim dt1 As DataTable = ds.Tables(0)
        Dim rel As DataRelation = ds.Relations(8)
        For Each row As DataRow In dt1.Rows
            For Each childrow As DataRow In row.GetChildRows(rel)
                For c As Integer = 0 To childrow.Table.Columns.Count - 1
                    Debug.Print(c & "--" & dt1.TableName & "," & rel.RelationName & "," & childrow.Table.Columns(c).ColumnName & ":" & childrow.Item(c))
                Next c
                For Each childrow2 As DataRow In childrow.GetChildRows(ds.Relations("Activity_Result"))
                    For c As Integer = 0 To childrow.Table.Columns.Count - 1
                        Debug.Print(c & "--" & childrow.Table.TableName & ",Activity_Result," & childrow2.Table.Columns(c).ColumnName & ":" & childrow2.Item(c))
                    Next c
                Next
            Next
        Next
        With Me.ComboBox1
            .Items.Clear()
            For Each dt As DataTable In ds.Tables
                .Items.Add(dt.TableName)
            Next
        End With
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.Text = "" Then Exit Sub
        Dim bs1 As New BindingSource
        With Me.DataGridView1
            .AutoGenerateColumns = True
            bs1.DataSource = ds
            bs1.DataMember = ComboBox1.Text
            .DataSource = bs1
        End With
        With Me.ComboBox2
            .Items.Clear()
            For Each dr As DataRelation In ds.Relations
                If dr.ParentTable.TableName = ComboBox1.Text Then .Items.Add(dr.RelationName)
            Next
            If .Items.Count > 0 Then .SelectedIndex = 0
        End With
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        If ComboBox2.Text = "" Then Exit Sub
        Dim bs2 As New BindingSource
        With Me.DataGridView2
            .AutoGenerateColumns = True
            bs2.DataSource = Me.DataGridView1.DataSource
            bs2.DataMember = ds.Relations(ComboBox2.Text).RelationName
            .DataSource = bs2
        End With
    End Sub
End Class